{extend name='public/base' /}
{block name='content'}
{include file='public/content_header' /}
<section class="content">
    <div class="row">
        <div class="col-md-12">
            <div class="card">
                <!-- 表单头部 -->
                <div class="card-header">
                    <div class="btn-group">
                        <a class="btn flat btn-sm btn-default BackButton">
                            <i class="fas fa-arrow-left"></i>
                            返回
                        </a>
                    </div>
                </div>
                <!-- 表单 -->
                <form id="dataForm" class="form-horizontal dataForm" action="" method="post"
                      enctype="multipart/form-data">
                    <!-- 表单字段区域 -->
                    <div class="card-body">
                        <div class="form-group row">
                            <label for="name" class="col-sm-2 col-form-label">名称</label>
                            <div class="col-sm-10 col-md-4 formInputDiv">
                                <input id="name" name="name" value="{$data.name|default=''}" placeholder="请输入名称"
                                       type="text" class="form-control fieldText">
                            </div>
                        </div>
                        <div class="form-group row">
                            <label for="img" class="col-sm-2 col-form-label">图片</label>
                            <div class="col-sm-10 col-md-4 formInputDiv">
                                <div class="input-group">
                                    <input id="img_file" name="img_file" type="file" class="file-loading "
                                           data-initial-preview="{if isset($data)}{$data.img}{/if}">
                                    <input name="img" id="img" value="{$data.img|default=''}" hidden placeholder="请上传图片"
                                           class="fieldImage">
                                    <script>
                                        initUploadImg('img');
                                    </script>
                                </div>
                            </div>
                        </div>
                        <div class="form-group row">
                            <label for="slide" class="col-sm-2 col-form-label">轮播</label>
                            <div class="col-sm-10 col-md-8 formInputDiv">
                                <div class="input-group">
                                    <input id="slide_file" name="slide_file" type="file" class="file-loading " multiple>
                                    <input name="slide" id="slide" value="{$data.slide|default=''}" hidden
                                           placeholder="请上传图片" class="fieldMultiImage">
                                    <script>
                                        initUploadMultiImg('slide');
                                    </script>
                                </div>
                            </div>
                        </div>
                        <div class="form-group row">
                            <label for="goods_category_id" class="col-sm-2 col-form-label">所属分类</label>
                            <div class="col-sm-10 col-md-4 formInputDiv">
                                <select name="goods_category_id" id="goods_category_id"
                                        class="form-control select2bs4 fieldSelect" data-placeholder="请选择所属分类">
                                    {$goods_category_list|raw|default=''}
                                </select>
                            </div>
                        </div>
                        <script>
                            $('#goods_category_id').select2({
                                theme: 'bootstrap4'
                            });
                        </script>
                        <div class="form-group row">
                            <label for="brand_id" class="col-sm-2 col-form-label">品牌</label>
                            <div class="col-sm-10 col-md-4 formInputDiv">
                                <select name="brand_id" id="brand_id" class="form-control select2bs4 fieldSelect"
                                        data-placeholder="请选择品牌">
                                    <option value=""></option>
                                    {foreach name='brand_list' id='item'}
                                    <option value="{$item.id}" {if isset($data) &&
                                            $data.brand_id==$item.id}selected{/if}>
                                        {$item.name}
                                    </option>
                                    {/foreach}
                                </select>
                            </div>
                        </div>
                        <script>
                            $('#brand_id').select2({
                                theme: 'bootstrap4'
                            });
                        </script>
                        <div class="form-group row">
                            <label for="origin_price" class="col-sm-2 col-form-label">原价</label>
                            <div class="col-sm-10 col-md-4 formInputDiv">
                                <div class="input-group">
                                    <input id="origin_price" name="origin_price"
                                           value="{$data.origin_price|default='0.00'}" placeholder="请输入原价" type="number"
                                           class="form-control fieldNumber">
                                </div>
                            </div>
                        </div>
                        <script>
                            $('#origin_price')
                                .bootstrapNumber({
                                    upClass: 'success',
                                    downClass: 'primary',
                                    center: true
                                });
                        </script>
                        <div class="form-group row">
                            <label for="price" class="col-sm-2 col-form-label">售价</label>
                            <div class="col-sm-10 col-md-4 formInputDiv">
                                <div class="input-group">
                                    <input id="price" name="price" value="{$data.price|default='0.00'}"
                                           placeholder="请输入售价" type="number" class="form-control fieldNumber">
                                </div>
                            </div>
                        </div>
                        <script>
                            $('#price')
                                .bootstrapNumber({
                                    upClass: 'success',
                                    downClass: 'primary',
                                    center: true
                                });
                        </script>

                        <div class="form-group row">
                            <label for="use_attr" class="col-sm-2 col-form-label">是否使用规格</label>
                            <div class="col-sm-10">
                                <input class="input-switch" id="use_attr" value="1" {if(isset($data) &&
                                       $data.use_attr==1)}checked{/if} type="checkbox"/>
                                <input class="switch fieldSwitch" placeholder="是否使用规格" name="use_attr"
                                       value="{$data.use_attr|default='0'}" hidden/>
                            </div>
                        </div>

                        <script>
                            $('#use_attr').bootstrapSwitch({
                                onText: "是",
                                offText: "否",
                                onColor: "success",
                                offColor: "danger",
                                onSwitchChange: function (event, state) {
                                    $(event.target).closest('.bootstrap-switch').next().val(state ? '1' : '0').change();
                                    changeAttrDivDisplay(state);
                                }
                            });
                        </script>

                        <div id="attrDiv">
                            {include file='goods/attr' /}
                        </div>

                        <div class="form-group row">
                            <label for="detail" class="col-sm-2 col-form-label">详情</label>
                            <div class="col-sm-10">
                                <div id="detailEditor">{$data.detail|raw|default='<p></p>'}</div>
                                <textarea id="detail" name="detail"
                                          style="display: none">{$data.detail|raw|default=''}</textarea>
                            </div>
                        </div>
                        <script>
                            var E = E || window.wangEditor;
                            if (editor_detail !== undefined) {
                                editor_detail.destroy();
                            }
                            var editor_detail = new E('#detailEditor');
                            editor_detail.config.zIndex = 1000;
                            editor_detail.config.uploadImgServer = '{:url("admin/file/editor")}';
                            editor_detail.config.uploadFileName = 'file';
                            editor_detail.create();
                            editor_detail.config.onchange = function (newHtml) {
                                $('#detail').val(newHtml);
                            };


                        </script>
                        <div class="form-group row">
                            <label for="weight" class="col-sm-2 col-form-label">重量</label>
                            <div class="col-sm-10 col-md-4 formInputDiv">
                                <input id="weight" name="weight" value="{$data.weight|default='0.00'}"
                                       placeholder="请输入重量" type="text" class="form-control fieldText">
                            </div>
                        </div>
                        <div class="form-group row">
                            <label for="stock" class="col-sm-2 col-form-label">库存</label>
                            <div class="col-sm-10 col-md-4 formInputDiv">
                                <input id="stock" name="stock" value="{$data.stock|default='0'}" placeholder="请输入库存"
                                       type="text" class="form-control fieldText">
                            </div>
                        </div>
                        <div class="form-group row">
                            <label for="sort_number" class="col-sm-2 col-form-label">排序(升序)</label>
                            <div class="col-sm-10 col-md-4 formInputDiv">
                                <div class="input-group">
                                    <input id="sort_number" name="sort_number"
                                           value="{$data.sort_number|default='1000'}" placeholder="请输入排序(升序)"
                                           type="number" class="form-control fieldNumber">
                                </div>
                            </div>
                        </div>
                        <script>
                            $('#sort_number')
                                .bootstrapNumber({
                                    upClass: 'success',
                                    downClass: 'primary',
                                    center: true
                                });
                        </script>
                        <div class="form-group row">
                            <label for="status" class="col-sm-2 col-form-label">是否上架</label>
                            <div class="col-sm-10 col-md-4 formInputDiv">
                                <input class="input-switch" id="status" value="1" {if(!isset($data)
                                       ||$data.status==1)}checked{/if} type="checkbox"/>
                                <input class="switch fieldSwitch" placeholder="是否上架" name="status"
                                       value="{$data.status|default='1'}" hidden/>
                            </div>
                        </div>

                        <script>
                            $('#status').bootstrapSwitch({
                                onText: "是",
                                offText: "否",
                                onColor: "success",
                                offColor: "danger",
                                onSwitchChange: function (event, state) {
                                    $(event.target).closest('.bootstrap-switch').next().val(state ? '1' : '0').change();
                                }
                            });
                        </script>
                    </div>
                    <!-- 表单底部 -->
                    <div class="card-footer">
                        <div class="row">
                            <div class="col-sm-2">
                            </div>

                            <div class="col-sm-10 col-md-4 formInputDiv">

                                <div class="btn-group">
                                    <button type="submit" class="btn btn-sm btn-primary dataFormSubmit">
                                        保 存
                                    </button>
                                </div>
                                <div class="btn-group">
                                    <button type="reset" class="btn btn-sm btn-default dataFormReset">
                                        重 置
                                    </button>
                                </div>

                                {if !isset($data)}
                                <div class="btn-group float-right createContinueDiv">
                                    <input type="checkbox" class="form-check-input" value="1" id="_create"
                                           name="_create" title="继续添加数据">
                                    <label class="form-check-label" for="_create">继续添加</label>
                                </div>
                                {/if}
                            </div>
                        </div>
                    </div>
                </form>
            </div>
        </div>
    </div>
</section>
<script>
    /** 表单验证 **/
    $('#dataForm').validate({
        rules: {
            'name': {
                required: true,

            },
            'img': {
                required: true,

            },
            'slide[]': {
                required: true,

            },
            'goods_category_id': {
                required: true,

            },
            'brand_id': {
                required: true,

            },
            'origin_price': {
                required: true,

            },
            'price': {
                required: true,

            },
            'attr': {
                required: true,

            },
            'detail': {
                required: true,

            },
            'weight': {
                required: true,

            },
            'stock': {
                required: true,

            },
            'sort_number': {
                required: true,

            },
            'status': {
                required: true,

            },

        },
        messages: {
            'name': {
                required: "名称不能为空",

            },
            'img': {
                required: "图片不能为空",

            },
            'slide[]': {
                required: "轮播不能为空",

            },
            'goods_category_id': {
                required: "所属分类不能为空",

            },
            'brand_id': {
                required: "品牌不能为空",

            },
            'origin_price': {
                required: "原价不能为空",

            },
            'price': {
                required: "售价不能为空",

            },
            'attr': {
                required: "规格不能为空",

            },
            'detail': {
                required: "详情不能为空",

            },
            'weight': {
                required: "重量不能为空",

            },
            'stock': {
                required: "库存不能为空",

            },
            'sort_number': {
                required: "排序(升序)不能为空",

            },
            'status': {
                required: "是否上架不能为空",

            },

        }
    });

    function changeAttrDivDisplay(state) {
        if (state) {
            $('#stock').attr('readonly', 'readonly');
            $('#attrDiv').show();
        } else {
            $('#stock').attr('readonly', false);
            $('#attrDiv').hide();
        }
    }
</script>

{if isset($data) && $data.use_attr==1}
<script>
    changeAttrDivDisplay(true);
</script>
{else/}
<script>
    changeAttrDivDisplay(false);
</script>
{/if}

{/block}